﻿using OrderSystem.Common;
using OrderSystem.Templet;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormQueryStuff : FormTemplet
    {
        public FormQueryStuff()
        {
            this.InitializeComponent();
        }

        private void FormQueryStuff_Load(object sender, EventArgs e)
        {
            this.cbType.SelectedIndex = 0;
            this.dtpDateStart.Enabled = false;
            this.dtpDateEnd.Enabled = false;
        }

        private void chkDateUsed_CheckedChanged(object sender, EventArgs e)
        {
            if (this.chkDateUsed.Checked)
            {
                this.dtpDateStart.Enabled = true;
                this.dtpDateEnd.Enabled = true;
            }
            else
            {
                this.dtpDateStart.Enabled = false;
                this.dtpDateEnd.Enabled = false;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            StuffCondition conditionForQuery = this.GetConditionForQuery();
            if (conditionForQuery != null)
            {
                FormStuffInfo formStuffInfo = new FormStuffInfo(conditionForQuery);
                formStuffInfo.MdiParent = base.MdiParent;
                formStuffInfo.Show();
                formStuffInfo.Activate();
            }
        }

        private StuffCondition GetConditionForQuery()
        {
            StuffCondition stuffCondition = new StuffCondition();
            StuffCondition result;
            if (this.cbType.Text.Trim() == "" && this.txtLength.Text.Trim() == "" && this.txtWidth.Text.Trim() == "" && this.txtQuantity.Text.Trim() == "" && !this.chkDateUsed.Checked && this.txtRemark.Text.Trim() == "")
            {
                MessageBox.Show("至少要输入一条查询信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                result = null;
            }
            else
            {
                if (this.cbType.Text.Trim().Length > 0)
                {
                    if (this.cbType.Text.Trim().Length > 32)
                    {
                        MessageBox.Show("类型 格式不正确，字符不能大于 32", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.cbType.Focus();
                        result = null;
                        return result;
                    }
                    stuffCondition.Type = this.cbType.Text.Trim();
                }
                if (this.txtLength.Text.Trim() != "")
                {
                    if (!Regex.IsMatch(this.txtLength.Text.Trim(), "^[0-9]{1,8}$") || Convert.ToInt32(this.txtLength.Text.Trim()) <= 0)
                    {
                        MessageBox.Show("长度 格式不正确，此处应该为大于零的正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtLength.Focus();
                        result = null;
                        return result;
                    }
                    stuffCondition.Length = Convert.ToInt32(this.txtLength.Text.Trim());
                }
                if (this.txtWidth.Text.Trim() != "")
                {
                    if (!Regex.IsMatch(this.txtWidth.Text.Trim(), "^[0-9]{1,8}$") || Convert.ToInt32(this.txtWidth.Text.Trim()) <= 0)
                    {
                        MessageBox.Show("宽度 格式不正确，此处应该为大于零的正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtWidth.Focus();
                        result = null;
                        return result;
                    }
                    stuffCondition.Width = Convert.ToInt32(this.txtWidth.Text.Trim());
                }
                if (this.txtQuantity.Text.Trim() != "")
                {
                    if (!Regex.IsMatch(this.txtQuantity.Text.Trim(), "^[0-9]{1,8}$") || Convert.ToInt32(this.txtQuantity.Text.Trim()) <= 0)
                    {
                        MessageBox.Show("数量 格式不正确，此处应该为大于零的正整数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtQuantity.Focus();
                        result = null;
                        return result;
                    }
                    stuffCondition.Quantity = Convert.ToInt32(this.txtQuantity.Text.Trim());
                }
                if (this.chkDateUsed.Checked)
                {
                    int num = this.dtpDateStart.Value.CompareTo(this.dtpDateEnd.Value);
                    if (num > 0)
                    {
                        MessageBox.Show("订购日期：后面的日期要大于或等于前面的日期", "提示!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.dtpDateStart.Focus();
                        result = null;
                        return result;
                    }
                    stuffCondition.DateStart = this.dtpDateStart.Value.ToShortDateString() + " 00:00:00";
                    stuffCondition.DateEnd = this.dtpDateEnd.Value.ToShortDateString() + " 23:59:59";
                }
                if (this.txtRemark.Text.Trim() != "")
                {
                    if (this.txtRemark.Text.Trim().Length > 64)
                    {
                        MessageBox.Show("备注 字符数量不能大于64", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        this.txtRemark.Focus();
                        result = null;
                        return result;
                    }
                    stuffCondition.Remark = this.txtRemark.Text.Trim();
                }
                result = stuffCondition;
            }
            return result;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
